package imageEditor.mapEditor.model;

import imageEditor.utility.ImageDatabase;
import imageEditor.utility.ImagePainer;
import imageEditor.utility.Point2d;
import imageEditor.utility.Shape;

import java.awt.Image;
import java.io.Serializable;
import java.util.ArrayList;

public class Map implements Serializable{
	private int[] img;
	private ArrayList<Shape> gates, platforms, stairs;
	private int id;
	private String name;
	private Point2d sizes;

	public Map(int[] i,int id, String name, Point2d sizes) {
		img=i;
		this.id=id;
		this.name=name;
		this.sizes=sizes;
		
		stairs=new ArrayList<Shape>();
		gates=new ArrayList<Shape>();
		platforms=new ArrayList<Shape>();
	}

	public Image getImage() {
		return ImageDatabase.getImageFromArray(img, sizes.getX(), sizes.getY());
	}

	public void update(ArrayList<Shape> shapes) {
		for(Shape s:shapes){
			if(s.getColor()==ImagePainer.GATE_COLOR)
				gates.add(s);
			else if(s.getColor()==ImagePainer.STAIR_COLOR)
				stairs.add(s);
			else if(s.getColor()==ImagePainer.PLATFORM_COLOR)
				platforms.add(s);
		}
	}
	
	public ArrayList<Shape> getShapes(){
		ArrayList<Shape> res=new ArrayList<Shape>(gates);
		res.addAll(stairs);
		res.addAll(platforms);
		
		return res;
		
	}

	public String getCode() {
		String res="";
		res+="\t\tMap map=new Map(new Point2d(), new Point2d("+sizes.getX()+","+sizes.getY()+"), \""+name+"\", new Id("+id+"));\n\n";
		
		int i=0;
		for(Shape g:gates){
			String[] tmp=g.getMessage().split("@");
			
			res+="\t\tGate gate"+i+"=new Gate(new Id("+tmp[0]+"), new Point2d("+g.getX()+","+g.getY()+"), new Point2d("+g.getBottomX()+", "+g.getBottomY()+"), new Point2d("+tmp[1]+"));\n";
			res+="\t\tmap1.addGate(gate"+i+");\n";
			
			i++;
		}
		
		res+="\n";
		
		i=0;
		for(Shape p:platforms){
			res+="\t\tPlatform platform"+i+"=new Platform(new Point2d("+p.getX()+","+p.getY()+"),new Point2d("+p.getBottomX()+", "+p.getBottomY()+"));\n";
			res+="\t\tmap.addPlatform(platform"+i+");\n";
			i++;
		}
		
		res+="\n";
		
		i=0;
		for(Shape s:stairs){
			res+="\t\tStair stair"+i+"=new Stair(new Point2d("+s.getX()+","+s.getY()+"),new Point2d("+s.getBottomX()+", "+s.getBottomY()+"));\n";
			res+="\t\tmap.addStair(platform"+i+");\n";
			i++;
		}
		
		return res;
	}

	public int[] getImageInt() {
		return img;
	}

	public int getWidth() {
		return sizes.getX();
	}

	public int getHeight() {
		return sizes.getY();
	}

	public void addPlatform(Shape s) {
		platforms.add(s);
	}
	
	public void addGate(Shape s) {
		gates.add(s);
	}
	
	public void addStair(Shape s) {
		stairs.add(s);
	}

	public void remove(Shape tmp) {
		if(gates.contains(tmp))
			gates.remove(tmp);
		else if(platforms.contains(tmp))
			platforms.remove(tmp);
		else if(stairs.contains(tmp))
			stairs.remove(tmp);
	}

}
